import UIKit

/* 数组 */
// 1
var courseArray1 = [String]()
courseArray1.append("数据结构")
courseArray1.append("计算机组成")
courseArray1.append("计算机网络")

var courseArray2 : [String] = ["iOS应用开发实践", "Swift程序设计", "人工智能"]

var courseArray3 = [String](repeating: "音乐", count: 1)
courseArray3.append("美术")
courseArray3.append("文学")

// 2
// Array<String>使用了泛型，[String]是Array<String>的一种简写的形式
var courseArray4 : Array<String>
courseArray4 = courseArray1 + courseArray2 + courseArray3

// 3
courseArray4[0] = "离散数学"
courseArray4.insert("移动应用开发实践", at: 6)
courseArray4.remove(at: 8)

// 4
for (index, value) in courseArray4.enumerated() {
    print("\(index) - \(value)")
}

// 5
var arraySlice = courseArray4[3...6]
print(arraySlice[3])
arraySlice[3] = "Android应用开发实践"
print(arraySlice[3])
print(courseArray4[3])

var sliceArray = Array(arraySlice)
print(sliceArray[0])
sliceArray[1] = "Kotlin程序设计"
print(sliceArray[1])
print(courseArray4[4])
// 相同之处
// 都可以对数组的操作方式对其进行修改
// 不同之处
// arraySlice中的下标引用与courseArray4中相同
// 在早期版本的Swift中，修改将直接影响原数组
// Swift5中已经不会影响到原数组
// sliceArray为一独立的数组，下标从0开始
// 修改不影响原数组

// 6
sliceArray.swapAt(sliceArray.startIndex, sliceArray.endIndex - 1)

for item in sliceArray {
    print(item)
}

// 7
sliceArray.sort()

for item in sliceArray {
    print(item)
}

// 8
print(sliceArray.contains("Swift"))
print(sliceArray.contains("Swift Programming"))

/* 集合 */
// 1
var gradeOfTheory = Set<String>()

if gradeOfTheory.isEmpty {
    gradeOfTheory.insert("Fail")
    gradeOfTheory.insert("Pass")
    gradeOfTheory.insert("Common")
    gradeOfTheory.insert("Good")
    gradeOfTheory.insert("Excellent")
} else {
    print("gradeOfTheory is not empty")
}

// 2
var gradeOfExperiment : Set<String> = gradeOfTheory
gradeOfExperiment.remove("Common")
gradeOfExperiment.remove("Good")
gradeOfExperiment.remove("Excellent")

if gradeOfExperiment.contains("Fail") {
    print("gradeOfExperiment contains Fail")
}

if gradeOfExperiment.contains("Pass") {
    print("gradeOfExperiment contains Pass")
}

// 3
for item in gradeOfTheory {
    print(item)
}

for item in gradeOfExperiment {
    print(item)
}

// 4
gradeOfTheory.sorted()
gradeOfExperiment.sorted()
for item in gradeOfTheory {
    print(item)
}

for item in gradeOfExperiment {
    print(item)
}

// 5
print(gradeOfExperiment.intersection(gradeOfTheory))
print(gradeOfExperiment.union(gradeOfTheory))
print(gradeOfExperiment.subtracting(gradeOfTheory))
// intersection和union方法并不会改变集合的内容
// 如果使用subtract方法，将直接从gradeOfExperiment中减去相应的元素

/* 字典 */
// 1
var collegeDict : Dictionary<Int, String> = [1: "材料科学与工程学院", 2: "电子信息工程学院", 3: "自动化科学与电气工程学院", 4:"能源与动力工程学院", 5: "航空科学与工程学院", 6: "计算机学院"];

// 2
collegeDict.updateValue("revisedSchool", forKey: 3)
collegeDict[4] = "updatedSchool"
print(collegeDict)

// 3
collegeDict.updateValue("Mechanism", forKey: 7)
collegeDict[8] = "Management"
// 可以
// updateValue()参数的key不存在，返回nil并且将给定的key-value对插入
// 下标访问法针对不存在的key操作能增加新的key-value对

// 4
for (key, value) in collegeDict {
    print("\(key) : \(value)")
}

// 5
var collegeDictKeys = [Int](collegeDict.keys)
var collegeDictValues = [String](collegeDict.values)

for item in collegeDictKeys {
    print(item)
}

for item in collegeDictValues {
    print(item)
}

/* 控制流 */
// 1
var addresult1 = 0;
for number in 1...100 {
    addresult1 += number
}
print(addresult1)

// 2
var addresult2 = 0
var number = 1;
while number <= 100 {
    addresult2 += number
    number += 1
}
print(addresult2)

var addresult3 = 0
number = 1
repeat {
    addresult3 += number
    number += 1
} while number <= 100
print(addresult3)

// 3
var score : Int = 75

if 0 <= score && score < 60 {
    print("Fail")
} else if 60 <= score && score < 70 {
    print("Pass")
} else if 70 <= score && score < 80 {
    print("Common")
} else if 80 <= score && score < 90 {
    print("Good")
} else if 90 <= score && score <= 100{
    print("Excellent")
} else {
    print("Error")
}

// 4
var grade: String
switch score {
case 0..<60 : grade = "Fail"
case 60..<70 : grade = "Pass"
case 70..<80 : grade = "Common"
case 80..<90 : grade = "Good"
case 90...100 : grade = "Excellent"
default : grade = "Error"
}

// 5
var subject = "Math"
grade = "Excellent"
var subjectInfo = (subject, grade)
switch subjectInfo {
case (_, "Fail") : print("grade等于Fail")
case (_, "Pass") : print("grade等于Pass")
case ("Math", "Excellent") : print("grade等于Excellent且subject等于Math")
case ("Physics", "Excellent") :
    print("grade等于Excellent且subject等于Physics")
default:
    print("没有匹配的条件")
}
